// components/searchbar/index.js
Component({
    options: {
        addGlobalClass: true,
        writeIdToDOM: true
    },
    properties: {
        extClass: {
            type: String,
            value: ''
        },
        focus: {
            type: Boolean,
            value: false
        },
        placeholder: {
            type: String,
            value: '搜索关键字'
        },
        value: {
            type: String,
            value: ''
        },
        throttle: {
            // 500ms内只会调用一次search函数
            type: Number,
            value: 500
        },
        cancelText: {
            type: String,
            value: '取消'
        },
        cancel: {
            type: Boolean,
            value: true
        }
    },
    data: {
        result: []
    },
    lastSearch: Date.now(),
    lifetimes: {
        attached() {
            if (this.data.focus) {
                this.setData({
                    searchState: true
                })
            }
        }
    },
    methods: {
        clearInput() {
            this.setData({
                value: '',
                focus: false,
                result: []
            })
            this.triggerEvent('clear')
        },
        inputFocus(e) {
            this.triggerEvent('focus', e.detail)
        },
        inputBlur(e) {
            this.setData({
                focus: false
            })
            this.triggerEvent('blur', e.detail)
        },
        inputChange(e) {
            this.setData({
                value: e.detail.value
            })
            this.triggerEvent('input', e.detail)
        },
        inputConfirm(e) {
            this.triggerEvent('search', e.detail.value)
        },
        search(){
            this.triggerEvent('search', this.data.value)
        }
    }
})
